|
@@ -20,25 +20,54 @@ script: jquery.showusername.js
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
// See the License for the specific language governing permissions and
|
|
// See the License for the specific language governing permissions and
|
|
|
// limitations under the License.
|
|
// limitations under the License.
|
|
|
-;(function($) {
|
|
|
|
|
- $.fn.showUsername = function(){
|
|
|
|
|
- return this.each(function(){
|
|
|
|
|
- var elem = $(this);
|
|
|
|
|
- if (elem.data("user")!=null && elem.data("user")["username"]){
|
|
|
|
|
- elem.text(elem.data("user")["username"]);
|
|
|
|
|
|
|
+;(function ($, window, document, undefined) {
|
|
|
|
|
+
|
|
|
|
|
+ var pluginName = "jHueUsername",
|
|
|
|
|
+ defaults = {
|
|
|
|
|
+ onLoad: function(){}
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ function Plugin(element, options) {
|
|
|
|
|
+ this.element = element;
|
|
|
|
|
+ this.options = $.extend({}, defaults, options) ;
|
|
|
|
|
+ this._defaults = defaults;
|
|
|
|
|
+ this._name = pluginName;
|
|
|
|
|
+ this.init();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Plugin.prototype.setOptions = function(options) {
|
|
|
|
|
+ this.options = $.extend({}, defaults, options) ;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ Plugin.prototype.init = function () {
|
|
|
|
|
+ var _this = this;
|
|
|
|
|
+ var elem = $(_this.element);
|
|
|
|
|
+ if (elem.data("user")!=null && elem.data("user")["username"]){
|
|
|
|
|
+ elem.text(elem.data("user")["username"]);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ $.post(
|
|
|
|
|
+ "/profile", function(user){
|
|
|
|
|
+ // sets a user field value the
|
|
|
|
|
+ elem.data("user", user);
|
|
|
|
|
+ elem.text(elem.data("user")["username"]);
|
|
|
|
|
+ _this.options.onLoad(user);
|
|
|
|
|
+ }, "json").error(function(e){
|
|
|
|
|
+ // we get an error when the user is not logged in
|
|
|
|
|
+ elem.data("user", null);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ $.fn[pluginName] = function (options) {
|
|
|
|
|
+ return this.each(function () {
|
|
|
|
|
+ if (!$.data(this, 'plugin_' + pluginName)) {
|
|
|
|
|
+ $.data(this, 'plugin_' + pluginName, new Plugin( this, options));
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- $.post(
|
|
|
|
|
- "/profile", function(user){
|
|
|
|
|
- // sets a user field value the
|
|
|
|
|
- elem.data("user", user);
|
|
|
|
|
- elem.text(elem.data("user")["username"]);
|
|
|
|
|
- }, "json").error(function(e){
|
|
|
|
|
- // we get an error when the user is not logged in
|
|
|
|
|
- elem.data("user", null);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ $.data(this, 'plugin_' + pluginName).setOptions(options);
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
-})(jQuery);
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+})(jQuery, window, document );
|